var ns, toast, UIActionProgress, UISearchBar;
var app = new Vue({
    el: '#app',
    data: {
        companyName: '', //公司名称
        postionName: "", //岗位
        userHeadImg: "", //用户头像
        userName: "", //用户名称
        bannerimg: '../image/header_bg.png',
        IsMain:1,
        list: [],
        TypeID:0,
        IsShow:false,
        form: {
            pageindex: 0,
            pagesize: 2,
        },
        showsearch:false,
        showTop:false,
        name:'',
        typeurl:'',
        moduleList:[],
        touchID:0,
        SortOrder:0,
        newCount:0,//消息未读数量

    },
    methods: {
        openSetting: function () {
            ns.openWin('xitongshezhi_frm', '系统设置');
        },
        get_data: function () {
            var that = this;
            var options = {};
            options.action = "gettasktypelist";
            options.IsMain=that.IsMain;
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    if (data.postionName) that.postionName = data.postionName; 
                    if(data.list.length>0){
                        that.list=data.list;
                    }
                    for(var i=0;i<data.list.length;i++){
                        if(data.list[i].AddTime){
                            data.list[i].AddTime = ns.SetTime_style(data.list[i].AddTime);
                        }else{
                            data.list[i].AddTime='';
                        }
                    }
                    that.newCount=data.TotalNum;

                } 
            });
        },
    
        get_user_data: function () {
            var that = this;
            // 公司名称
            var companyName = api.getPrefs({
                sync: true,
                key: 'companyName'
            });
            // 岗位名称
            var positionname = api.getPrefs({
                sync: true,
                key: 'positionname'
            });
            // 用户名称
            var username = api.getPrefs({
                sync: true,
                key: 'username'
            });
            // 用户头像
            var headimg = api.getPrefs({
                sync: true,
                key: 'headimg'
            });
            that.companyName = companyName || '重庆永友网络科技有限公司';
            that.postionName = positionname ? positionname : "";
            that.userHeadImg = headimg || '../image/default.png';
            // that.userHeadImg =  '../image/default.png';
            that.userName = username || '匿名用户';
        },
        do_open_scan_login: function (item) {
            var that = this;
            ns.confirmPer('camera', function () {
                setTimeout(function () {
                    that.do_open_scan_login_start();
                }, 200)
            })
        },
        do_open_scan_login_start: function () {
            var that = this;
            ns.openFrame('scanner_frm', {
                type: 0
            });
        },

        //打开配置页面
        do_open_peizhi:function(){
            var that = this;
            that.showsearch=!that.showsearch;   
            that.IsMain=2;
            that.get_data();
        },
        do_select_open:function(ID,isshow){
            var that=this;
            that.list.IsShow=!isshow;
        },
        //点击遮罩关闭弹窗
        doHideSearch:function(){
            var that=this;
            that.showsearch = false;
            that.showTop=false;
            that.IsMain=1;
            that.get_data();
        },
        //确定配置项目
        do_sure:function(){
            var that=this;
            that.showsearch = false;
            that.IsMain=1;
            that.get_data();
        },
        selectType:function(id,isshow){
            var that=this;
            // var isshow=!isshow;
            
            that.moduleList.push({
                ID:id,
                IsShow:isshow,
            })
            var options = {};
            options.action = "savemodulesuctom";
            options.moduleList = JSON.stringify(that.moduleList);
            ns.post(options, function(succeed, data, err) {
                if (data.status==true) {
                    api.toast({
                        msg:'成功',
                        duration: 2000,
                        location: 'bottom'
                    });
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
            
        },
        //长按弹出操作框
        gettouchStart:function(index,id,name,IsShow,SortOrder){
            var that = this;
            api.addEventListener({
                name:'longpress'
            }, function(ret){               
                that.showTop=!that.showTop;  
                that.name=name;
                that.touchID=id;
                that.IsShow=IsShow;
                that.SortOrder=SortOrder;
            })
        },
        //置顶
        Totoplist:function(){
            var that=this;
            that.moduleList=[];
            that.moduleList.push({
                ID:that.touchID,
                SortOrder:1,
                IsShow:that.IsShow,
            });
            var options = {};
            options.action = "savemodulesuctom";
            options.moduleList = JSON.stringify(that.moduleList);
            options.IsSortOrder=1;
            ns.post(options, function(succeed, data, err) {
                that.showTop=false;
                if (data.status==true) {
                    
                    that.get_data();
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });

        },
        //删除
        deletelist:function(){
            var that=this;
            var isshow=!that.IsShow;
            that.moduleList=[];
            that.moduleList.push({
                ID:that.touchID,
                SortOrder:that.SortOrder,
                IsShow:isshow,
            });
            var options = {};
            options.action = "savemodulesuctom";
            options.moduleList = JSON.stringify(that.moduleList);
            options.IsSortOrder=0;
            ns.post(options, function(succeed, data, err) {
                that.showTop=false;
                if (data.status==true) {
                    api.toast({
                        msg:'删除成功',
                        duration: 2000,
                        location: 'bottom'
                    });
                    that.get_data();
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },

        do_opentypelist:function(ID){
            var title;
            if(ID==814){
                title="客服工单待办";
                ns.openWin('customerservice_renwulist',title,{
                    IsWorkOrder:1,
                    servicetype:1
                });
                return;
            }
            if(ID==695){
                title="投诉建议";
                ns.openWin('customerservice_renwulist',title,{
                    IsWorkOrder:2,
                    servicetype:2
                });
                return;
            }
            if(ID==690){
                title="设备巡检待办";
                ns.openWin('equipment_renwulist',title);
                return;
            }
            if(ID==692){
                title="安防巡更待办";
                ns.openWin('patrolplan_renwulist',title);
                return;
            }
            if(ID==693){
                title="保洁绿化待办";
                ns.openWin('bjlh_renwulist',title);
                return;
            }
            if(ID==963){
                title="通知公告";
                ns.openWin('tongzhigonggao_renwulist',title);
                return;
            }
            if(ID==956){
                title="审批待办";
                ns.openWin('examine_renwulist',title);
                return;
            }
            if(ID==1064){
                title="内部通知";
                ns.openWin('neibutz_renwulist',title);
                return;
            }

        },
        //打开个人中心
        openSetting: function () {
            ns.openWin('gerenzhongxin_frm', '个人信息');
        },
        //打开搜索
        do_open_sousuo:function(){
            ns.openWin('home_sousuo', '搜索中心');
        },
         //打开消息中心
         do_open_news:function(){
            ns.openWin('home_news_center', '消息中心');
        },
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    UIActionProgress = api.require('UIActionProgress');
    
    toast = new auiToast();
    setTimeout(function () {
        app.get_data();
    }, 500);
    setTimeout(function () {
        appupgrade.check_upgrade(false);
    }, 5000)
    ns.init_pull(true);
    app.get_user_data();
    api.addEventListener({
        name: 'changecentersuccess'
    }, function () {
        app.get_user_data();
    });
    
    api.addEventListener({
        name: 'isreadtrue'
    }, function (ret, err) {
        if (ret) {
            setTimeout(function () {
                app.get_data();
            }, 500);
        } else {
            api.toast({
                msg: err,
                duration: 2000,
                location: 'bottom'
            });
        }
    });
    api.addEventListener({
        name: 'isreadCustomer'
    }, function () {
        app.get_data();
    });
    api.addEventListener({
        name: 'isreadEquipment'
    }, function () {
        app.get_data();
    });
    api.addEventListener({
        name: 'isreadPatrol'
    }, function () {
        app.get_data();
    });
    api.addEventListener({
        name: 'isreadBjlh'
    }, function () {
        app.get_data();
    });
    api.addEventListener({
        name: 'isreadTongzhi'
    }, function () {
        app.get_data();
    });
    api.addEventListener({
        name: 'isreadExamine'
    }, function () {
        app.get_data();
    });
    api.addEventListener({
        name: 'home_new_updata'
    }, function () {
        app.get_data();
    });
    api.addEventListener({
        name: 'purchase_updata'
    }, function () {
        app.get_data();
    });
}
